<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商铺编号" prop="resourceName">
        <el-input
          v-model="queryParams.resourceName"
          placeholder="请输入商铺编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收费项目" prop="feeItemId">
        <el-select v-model="queryParams.feeItemId" placeholder="请选择收费项目" clearable>
          <el-option
            v-for="item in feeItemList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户姓名" prop="feeUser">
        <el-input
          v-model="queryParams.feeUser"
          placeholder="请输入客户姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="付款方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择付款方式" clearable>
          <el-option
            v-for="dict in dict.type.fee_method_options"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.pay_order_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pay:deposit:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pay:deposit:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="depositList">
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="资源名" align="center" prop="resourceName"/>
      <el-table-column label="客户姓名" align="center" prop="feeUser"/>
      <el-table-column label="收费项名" align="center" prop="feeItemName"/>
      <el-table-column label="金额" align="center" prop="amt"/>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_order_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="付款方式" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fee_method_options" :value="scope.row.payType"/>
        </template>
      </el-table-column>
      <el-table-column label="退费方式" align="center" prop="refundType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fee_method_options" :value="scope.row.refundType"/>
        </template>
      </el-table-column>
      <el-table-column label="收款人" align="center" prop="operateUser"/>
      <el-table-column label="收款时间" align="center" prop="operateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款人" align="center" prop="refundUser"/>
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="支付单号" align="center" prop="payNo"/>
      <el-table-column label="退款单号" align="center" prop="refundNo"/>

      <el-table-column label="操作" width="240px" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            :disabled="scope.row.state!='2'"
            type="text"
            icon="el-icon-edit"
            @click="handleRefund(scope.row)"
          >退款
          </el-button>
          <el-button
            size="mini"
            :disabled="scope.row.state!='0'"
            type="text"
            icon="el-icon-edit"
            @click="handleConfirm(scope.row)"
          >确认收款
          </el-button>
          <el-dropdown>
                                <span class="el-dropdown-link">
                                    <el-button type="text" icon="el-icon-printer" size="mini">
                                        打印
                                        <i class="el-icon-arrow-down el-icon--right"></i>
                                    </el-button>
                                </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handlePrint(scope.row)" v-if="scope.row.state == '5'||scope.row.state=='2'">打印收款单</el-dropdown-item>
              <el-dropdown-item v-if="scope.row.state == '5'"
                                @click.native="handleRefundPrint(scope.row.refundNo)">
                打印退款单
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加临时收费对话框 -->
    <el-dialog :title="title" :visible.sync="openAdd" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户类型">
          <el-radio-group v-model="form.userType">
            <el-radio :label="1">商铺客户</el-radio>
            <el-radio :label="2">非商铺客户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="商铺编号" prop="resourceName" v-if="form.userType==1">
          <el-select v-model="form.resourceId" style="width: 100%" filterable placeholder="请选择">
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.stateCompany }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收费项目" prop="feeItemId">
          <el-select v-model="form.feeItemId" style="width: 100%" placeholder="请选择收费项目" clearable>
            <el-option
              v-for="item in feeItemList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="feeUser">
          <el-input v-model="form.feeUser" placeholder="请输入客户姓名"/>
        </el-form-item>
        <el-form-item label="金额" prop="amt">
          <el-input v-model="form.amt" placeholder="请输入金额"/>
        </el-form-item>
        <el-form-item label="付款方式" prop="payType">
          <el-select v-model="form.payType" style="width: 100%" placeholder="请选择付款方式">
            <el-option
              v-for="dict in dict.type.fee_method_options"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 临时收费退费对话框 -->
    <el-dialog :title="title" :visible.sync="openRefund" width="500px" append-to-body>
      <el-form ref="formRefund" :model="formRefund" :rules="rules" label-width="80px">
        <el-form-item label="退款方式" prop="refundType">
          <el-select v-model="formRefund.refundType" placeholder="请选择付款方式">
            <el-option
              v-for="dict in dict.type.fee_method_options"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formRefund.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRefundForm">确 定</el-button>
        <el-button @click="openRefund=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listTemp, getTemp, addTemp, refundTemp,confirmPay} from "@/api/pay/temp";
import {getTempPayFeeItem} from "@/api/pay/feeItem";
import {getAllHouses} from "@/api/system/house";

export default {
  name: "Temp",
  dicts: ['fee_method_options', 'pay_order_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 临时收费表格数据
      depositList: [],
      // 弹出层标题
      title: "",
      // 是否显示添加临时收费弹出层
      openAdd: false,
      //是否显示退费的弹出层
      openRefund: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        resourceName: null,
        block: null,
        feeItemId: null,
        feeUser: null,
        amt: null,
        payType: null,
        refundType: null,
        operateUser: null,
        operateTime: null,
        refundUser: null,
        refundTime: null,
        payNo: null,
        refundNo: null,
        state: null,
      },
      // 表单参数
      form: {
        userType: "1"
      },
      //退费表单
      formRefund: {},
      // 表单校验
      rules: {
        feeItemId: [
          { required: true, message: "收费项目ID不能为空", trigger: "blur" }
        ],
        feeItemName: [
          { required: true, message: "收费项名不能为空", trigger: "blur" }
        ],
        payType: [
          { required: true, message: "支付类型不能为空", trigger: "blur" }
        ],
        refundType: [
          { required: true, message: "退费类型不能为空", trigger: "blur" }
        ]
      },
      //收费项目
      feeItemList: [],
      //house列表
      houseList: [],
      //是否退款的弹出层
      isRefund: false
    };
  },
  created() {
    this.getList();
    this.loadPayFeeItemByType();
    //加载所有house
    this.loadAllHouses();
  },
  methods: {
    //加载所有house
    loadAllHouses() {
      getAllHouses().then(res => {
        this.houseList = res.data;
      })
    },
    //根据类型查询收费项目
    loadPayFeeItemByType() {
      getTempPayFeeItem().then(res => {
        this.feeItemList = res.data;
      })
    },
    /** 查询临时收费列表 */
    getList() {
      this.loading = true;
      listTemp(this.queryParams).then(response => {
        this.depositList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openAdd = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userType: 1,
        resourceName: null,
        resourceId: null,
        block: null,
        feeItemId: null,
        feeItemName: null,
        feeUser: null,
        amt: null,
        payType: null,
        remark: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openAdd = true;
      this.title = "添加临时收费";
    },
    /** 退费按钮操作 */
    handleRefund(row) {
      this.reset();
      const id = row.id
      getTemp(id).then(response => {
        this.formRefund = response.data;
        this.openRefund = true;
        this.title = `给【${row.feeUser}的${row.resourceName}】退款`;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //因为项目类型选择之后只有ID 给form里面加一个项目名称
          let feeItemName = "";
          this.feeItemList.filter(item => {
            if (item.id == this.form.feeItemId) {
              feeItemName = item.name;
            }
          })
          //赋值给form
          this.form.feeItemName = feeItemName;
          if (this.form.userType == 1) {
            let resourceName = "";
            let block = "";
            this.houseList.filter(item => {
              if (item.id == this.form.resourceId) {
                resourceName = item.name;
                block = item.blockId;
              }
            })
            this.form.resourceName = resourceName;
            this.form.block = block;
          }
          addTemp(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.openAdd = false;
            this.getList();
          });
        }
      });
    },
    //提交退费
    submitRefundForm() {
      this.$refs["formRefund"].validate(valid => {
        if (valid) {
          refundTemp(this.formRefund).then(response => {
            this.$modal.msgSuccess("退费成功");
            this.openRefund = false;
            this.getList();
          });
        }
      });
    },
    /** 确认收款操作 */
    handleConfirm(row){
      this.$modal.confirm(`是否确认【${row.resourceName}】单位【${row.feeUser}的${row.resourceName}】收费？`).then(function() {
        return confirmPay(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("确认成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pay/temp/export', {
        ...this.queryParams
      }, `deposit_${new Date().getTime()}.xlsx`)
    },
    /** 打印收款单 */
    handlePrint() {
      this.download('pay/temp/export', {
        ...this.queryParams
      }, `deposit_${new Date().getTime()}.xlsx`)
    },
    /** 打印退款单 */
    handleRefundPrint() {
      this.download('pay/temp/export', {
        ...this.queryParams
      }, `deposit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

